$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'busi/b63averagespeedpeak/listdistrict',
        datatype: "json",
        colModel: [			
			{ label: '序号', name: 'averagespeedId', index: 'averagespeed_id', width: 50, key: true },
            { label: '高峰速度', name: 'averageSpeed', index: 'average_speed', width: 80 },
			{ label: '道路类型', name: 'roadTypeId', index: 'road_type_id', width: 80 },
			{ label: '车辆类型,参照63_vehicle_Type', name: 'vehicleTypeId', index: 'vehicle_type_id', width: 80 }


        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        flag:true,
        vehicleTypeList:{},
        roadTypeList:{},
		b63AverageSpeedPeak: {}
	},
    watch: {
        // fileupload: function (val) {
        //     // this.b63AverageSpeedPeak.filePath = val + ' ' + this.lastName
        // }
    },
	methods: {
		query: function () {
			vm.reload();
		},

		update: function (event) {
			var averagespeedId = getSelectedRow();
			if(averagespeedId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.flag=true;
            this.getRoadTypeList();
            this.getVehicleTypeList();
            
            this.getInfo(averagespeedId);
		},
		saveOrUpdate: function (event) {
            if(!$('form').data("bootstrapValidator").isValid()){
                alert("表单验证没通过");
                return;
            };
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {

                var url = vm.b63AverageSpeedPeak.averagespeedId == null ? "busi/b63averagespeedpeak/save" : "busi/b63averagespeedpeak/update";
                //alert($("#uploadfilename").val());
                //vm.b63AverageSpeedPeak.filepath=$("#uploadfilename").val();
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.b63AverageSpeedPeak),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();

                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var averagespeedIds = getSelectedRows();
			if(averagespeedIds == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "busi/b63averagespeedpeak/delete",
                        contentType: "application/json",
                        data: JSON.stringify(averagespeedIds),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(averagespeedId){
			$.get(baseURL + "busi/b63averagespeedpeak/info/"+averagespeedId, function(r){
                vm.b63AverageSpeedPeak = r.b63AverageSpeedPeak;
            });
		},
        getRoadTypeList:function(){
            $.get(baseURL + "busi/b63roadtype/select", function(r){
                vm.roadTypeList = r.list;
            });
        },
        getVehicleTypeList:function(){
            $.get(baseURL + "busi/b63vehicletype/select", function(r){
                vm.vehicleTypeList = r.list;
            });
        },

		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}

	}

});

//验证部分
$(function () {
    $('form').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            averageSpeed: {
                message: '指标解释填写失败',
                validators: {
                    notEmpty: {
                        message: '速度信息不能为空'
                    },
                    numeric: {
                        message: '速度为数值类型'
                    },
                    lessThan: {
                        value: 100,
                        message: "最大值为100"

                    },
                    greaterThan: {
                        value: 10,
                        message: "最小值为10"
                    }
                }

            }
        }

    });
});

// Validate the form manually
$('#validateBtn').click(function() {
    $('form').bootstrapValidator('validate');
});

$('#resetBtn').click(function() {
    $('form').data('bootstrapValidator').resetForm(true);
});


// 文件上传组件
$("#form_file").fileinput({
    uploadUrl:baseURL + "sys/file/upload", //接受请求地址
    uploadAsync : true, //默认异步上传
    showUpload : true, //是否显示上传按钮,跟随文本框的那个
    showRemove : true, //显示移除按钮,跟随文本框的那个
    showCaption : true,//是否显示标题,就是那个文本框
    showPreview : true, //是否显示预览,不写默认为true
    dropZoneEnabled : false,//是否显示拖拽区域，默认不写为true，但是会占用很大区域
    //minImageWidth: 50, //图片的最小宽度
    //minImageHeight: 50,//图片的最小高度
    //maxImageWidth: 1000,//图片的最大宽度
    //maxImageHeight: 1000,//图片的最大高度
    //maxFileSize: 0,//单位为kb，如果为0表示不限制文件大小
    //minFileCount: 0,
    maxFileCount : 1, //表示允许同时上传的最大文件个数
    enctype : 'multipart/form-data',
    validateInitialCount : true,
    previewFileIcon : "<i class='glyphicon glyphicon-king'></i>",
    msgFilesTooMany : "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
    allowedFileExtensions : [ 'zip','xlsx','xls','pdf','docx','doc' ],//配置允许文件上传的类型
    // allowedPreviewTypes : [ 'image' ],//配置所有的被预览文件类型
    // allowedPreviewMimeTypes : [ 'jpg', 'png', 'gif' ],//控制被预览的所有mime类型
    language : 'zh'
})
//异步上传返回结果处理
$('#form_file').on('fileerror', function(event, data, msg) {
    console.log("fileerror");
    console.log(data);
});
//异步上传返回结果处理
$("#form_file").on("fileuploaded", function(event, data, previewId, index) {
    console.log("fileuploaded");
    var response = data.response;
    console.log(data.files[0].name);
    alert("已上传文件:"+response.filename);
    vm.b63AverageSpeedPeak.areaNo=response.filename;
    //$("#uploadfilename").val(response.filename);
    //$("#uploadfilename").trigger('change');




    // $("#fileMd5").val(response.fileMd5);
    // $("#version").val(response.newVersionName);
    // $("#url").val(response.filePath);

});

//上传前
$('#form_file').on('filepreupload', function(event, data, previewId, index) {
    console.log("filepreupload");
    console.log(data.files[0].name);
    vm.b63AverageSpeedPeak.filePath=data.files[0].name;

});